跳到主要内容

Using LangChain

The simplest way is to directly set environment variables as shown below

API_SECRET_KEY = "sk-pvMtoVO******66249058b93C766F2D70167"
BASE_URL = "https://aihubmix.com/v1"; #Base URL for aihubmix

os.environ["OPENAI_API_KEY"] = API_SECRET_KEY
os.environ["OPENAI_BASE_URL"] = BASE_URL

Note: Ensure to add /v1 at the end of openai_api_base,


llm = ChatOpenAI(
openai_api_base="https://aihubmix.com/v1", # Note, add /v1 at the end
openai_api_key="sk-3133f******fee269b71d",
)

res = llm.predict("hello")

print(res)

Example code for using LLM to make predictions
The core is actually in setting the key and URL
Methods include:

  1. Setting using environment variables
  2. Passing in variables
  3. Manually setting environment variables
import requests
import time
import json
import time

from langchain.llms import OpenAI

API_SECRET_KEY = "your key from aihubmix";
BASE_URL = "https://aihubmix.com/v1"; #Base URL for aihubmix

os.environ["OPENAI_API_KEY"] = API_SECRET_KEY
os.environ["OPENAI_API_BASE"] = BASE_URL

def text():
llm = OpenAI(temperature=0.9)
text = "What would be a good company name for a company that makes colorful socks?"
print(llm(text))

if __name__ == '__main__':
text();

After running, you can see the return:

Lively Socks.